package com.aimao.blog.service.impl;

import com.aimao.blog.constant.ServerResponse;
import com.aimao.blog.dao.CommentMapper;
import com.aimao.blog.pojo.Comment;
import com.aimao.blog.service.CommentAdminService;
import com.aimao.blog.vo.CommentAndReply;
import com.aimao.blog.vo.CommentSearchResultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author PuaChen
 * @Create 2018-07-03 16:03
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class CommentAdminServiceImpl implements CommentAdminService {

    @Autowired
    private CommentMapper commentMapper;


    @Cacheable(value = "commentData")
    @Override
    public ServerResponse<PageInfo<CommentAndReply>> findAll(Integer articleId, boolean isSplit, Integer page, Integer size) {
        if (articleId == null) {
            return ServerResponse.createFailure("未提供文章ID");
        }
        if (isSplit) {
            if (page == null || size == null) {
                return ServerResponse.createFailure("参数错误");
            }
            PageHelper.startPage(page, size);
            PageHelper.orderBy("createTime desc");
        }
        List<Comment> comments = commentMapper.selectCommentAll(articleId);
        PageInfo pageInfo = new PageInfo(comments);
        List<CommentAndReply> list = new ArrayList<>();

        comments.forEach(item -> {
            CommentAndReply commentAndReply = new CommentAndReply();
            commentAndReply.setMain(item);
            commentAndReply.setReply(commentMapper.selectReplyAll(item.getArticleid(), item.getId()));
            list.add(commentAndReply);
        });
        pageInfo.setList(list);
        return ServerResponse.createSuccessAndData(pageInfo);
    }

    @CacheEvict(value = "commentData",allEntries = true)
    @Override
    public ServerResponse updateComment(Integer id, String content) {
        if (id == null || content == null) {
            return ServerResponse.createFailure("参数错误");
        }
        Comment comment = new Comment();
        comment.setId(id);
        comment.setContent(content);
        int row = commentMapper.updateByPrimaryKeySelective(comment);
        if (row <= 0) {
            return ServerResponse.createFailure("更新失败");
        }
        return ServerResponse.createSuccess();
    }

    @CacheEvict(value = "commentData",allEntries = true)
    @Override
    public ServerResponse deleteComment(Integer id) {
        if (id == null) {
            return ServerResponse.createFailure("参数错误");
        }
        int row = commentMapper.deleteByPrimaryKey(id);
        if (row <= 0) {
            return ServerResponse.createFailure("删除失败");
        }
        row = commentMapper.deleteByParentId(id);
        if (row <= 0) {
            return ServerResponse.createFailure("所属回复内容删除失败-请联系管理员");
        }
        return ServerResponse.createSuccess();
    }

    @CacheEvict(value = "commentData",allEntries = true)
    @Override
    public ServerResponse replyComment(Integer parentId, Comment comment) {
        if (parentId == null || comment == null || comment.getArticleid() == null) {
            return ServerResponse.createFailure("参数错误");
        }
        comment.setCreatetime(new Date());
        comment.setId(null);
        comment.setParentid(parentId);
        int row = commentMapper.insertSelective(comment);
        if (row <= 0) {
            return ServerResponse.createFailure("评论失败");
        }
        return ServerResponse.createSuccess();
    }

    @Override
    public ServerResponse<List<CommentSearchResultVo>> findNewCommentList(Integer size) {
        if (size == null) {
            return ServerResponse.createFailure("参数错误");
        }
        return ServerResponse.createSuccessAndData(commentMapper.selectCommentSearchList(size));
    }

    @Override
    public ServerResponse<List<CommentSearchResultVo>> findCommentByKeyWord(String key) {
        if (StringUtils.isBlank(key)) {
            return ServerResponse.createFailure("参数错误");
        }
        key = "%" + key + "%";
        return ServerResponse.createSuccessAndData(commentMapper.selectCommentSearchByArticleKey(key));
    }

}
